#include <stdio.h>

/*
 * 虚拟方法
 */

class object
{
	public:
			int color;
			
			object() 
			{
			
			}

			object(int color) 
			{
				this->color = color;
			}

			virtual void dump()
			{
				printf("color=%d\n", color);
			}
};

class box : public object
{
	private:
			int width, height, depth;
	public:
			box(int color, int width, int height, int depth)
			{
				this->color = color;
				this->width = width;
				this->height = height;
				this->depth = depth;
			}

			void dump()
			{
				printf("this is box.color=%d width=%d height=%d depth=%d\n", 
								color, width, height, depth);
			}
};

class sphere : public object
{
	private:
			int radius;
	public:
			sphere(int color, int radius)
			{
				this->color = color;
				this->radius = radius;
			}

			void dump()
			{
				printf("this is shpere. color=%d radius=%d\n", color, radius);
			}
};

int main()
{
	box b(1, 10, 20, 30);
	sphere s(2, 40);

	object *o1 = &b;
	object *o2 = &s;

	o1->dump();
	o2->dump();

	return 0;
}

#if 0

在调用虚拟函数时，编译器阶段可能无法确定对象的类型情况。当类中含有虚函数时，其基类的指针就可以指向任何派生类的
对象，这时就有可能不知道基类指针到底指向的是哪个对象的情况。这时就要根据实时类型信息，确实应当调用的相应函数。


#endif

#if 0
/*
 * intel
 */
0000000000001169 <main>:
    1169:	f3 0f 1e fa          	endbr64 
    116d:	55                   	push   %rbp
    116e:	48 89 e5             	mov    %rsp,%rbp
    1171:	48 83 ec 40          	sub    $0x40,%rsp
    1175:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    117c:	00 00 
    117e:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    1182:	31 c0                	xor    %eax,%eax
    1184:	48 8d 45 e0          	lea    -0x20(%rbp),%rax
    1188:	41 b8 1e 00 00 00    	mov    $0x1e,%r8d
    118e:	b9 14 00 00 00       	mov    $0x14,%ecx
    1193:	ba 0a 00 00 00       	mov    $0xa,%edx
    1198:	be 01 00 00 00       	mov    $0x1,%esi
    119d:	48 89 c7             	mov    %rax,%rdi
    11a0:	e8 b3 00 00 00       	callq  1258 <_ZN3boxC1Eiiii>
    11a5:	48 8d 45 d0          	lea    -0x30(%rbp),%rax
    11a9:	ba 28 00 00 00       	mov    $0x28,%edx
    11ae:	be 02 00 00 00       	mov    $0x2,%esi
    11b3:	48 89 c7             	mov    %rax,%rdi
    11b6:	e8 45 01 00 00       	callq  1300 <_ZN6sphereC1Eii>
    11bb:	48 8d 45 e0          	lea    -0x20(%rbp),%rax
    11bf:	48 89 45 c0          	mov    %rax,-0x40(%rbp)
    11c3:	48 8d 45 d0          	lea    -0x30(%rbp),%rax
    11c7:	48 89 45 c8          	mov    %rax,-0x38(%rbp)
    11cb:	48 8b 45 c0          	mov    -0x40(%rbp),%rax
    11cf:	48 8b 00             	mov    (%rax),%rax
    11d2:	48 8b 10             	mov    (%rax),%rdx
    11d5:	48 8b 45 c0          	mov    -0x40(%rbp),%rax
    11d9:	48 89 c7             	mov    %rax,%rdi
    11dc:	ff d2                	callq  *%rdx
    11de:	48 8b 45 c8          	mov    -0x38(%rbp),%rax
    11e2:	48 8b 00             	mov    (%rax),%rax
    11e5:	48 8b 10             	mov    (%rax),%rdx
    11e8:	48 8b 45 c8          	mov    -0x38(%rbp),%rax
    11ec:	48 89 c7             	mov    %rax,%rdi
    11ef:	ff d2                	callq  *%rdx
    11f1:	b8 00 00 00 00       	mov    $0x0,%eax
    11f6:	48 8b 4d f8          	mov    -0x8(%rbp),%rcx
    11fa:	64 48 33 0c 25 28 00 	xor    %fs:0x28,%rcx
    1201:	00 00 
    1203:	74 05                	je     120a <main+0xa1>
    1205:	e8 66 fe ff ff       	callq  1070 <__stack_chk_fail@plt>
    120a:	c9                   	leaveq 
    120b:	c3                   	retq   

000000000000120c <_ZN6objectC1Ev>:
    120c:	f3 0f 1e fa          	endbr64 
    1210:	55                   	push   %rbp
    1211:	48 89 e5             	mov    %rsp,%rbp
    1214:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)
    1218:	48 8d 15 49 2b 00 00 	lea    0x2b49(%rip),%rdx        # 3d68 <_ZTV6object+0x10>
    121f:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1223:	48 89 10             	mov    %rdx,(%rax)
    1226:	90                   	nop
    1227:	5d                   	pop    %rbp
    1228:	c3                   	retq   
    1229:	90                   	nop

000000000000122a <_ZN6object4dumpEv>:
    122a:	f3 0f 1e fa          	endbr64 
    122e:	55                   	push   %rbp
    122f:	48 89 e5             	mov    %rsp,%rbp
    1232:	48 83 ec 10          	sub    $0x10,%rsp
    1236:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)
    123a:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    123e:	8b 40 08             	mov    0x8(%rax),%eax
    1241:	89 c6                	mov    %eax,%esi
    1243:	48 8d 3d be 0d 00 00 	lea    0xdbe(%rip),%rdi        # 2008 <_IO_stdin_used+0x8>
    124a:	b8 00 00 00 00       	mov    $0x0,%eax
    124f:	e8 0c fe ff ff       	callq  1060 <printf@plt>
    1254:	90                   	nop
    1255:	c9                   	leaveq 
    1256:	c3                   	retq   
    1257:	90                   	nop

0000000000001258 <_ZN3boxC1Eiiii>:
    1258:	f3 0f 1e fa          	endbr64 
    125c:	55                   	push   %rbp
    125d:	48 89 e5             	mov    %rsp,%rbp
    1260:	48 83 ec 20          	sub    $0x20,%rsp
    1264:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)
    1268:	89 75 f4             	mov    %esi,-0xc(%rbp)
    126b:	89 55 f0             	mov    %edx,-0x10(%rbp)
    126e:	89 4d ec             	mov    %ecx,-0x14(%rbp)
    1271:	44 89 45 e8          	mov    %r8d,-0x18(%rbp)
    1275:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1279:	48 89 c7             	mov    %rax,%rdi
    127c:	e8 8b ff ff ff       	callq  120c <_ZN6objectC1Ev>
    1281:	48 8d 15 c8 2a 00 00 	lea    0x2ac8(%rip),%rdx        # 3d50 <_ZTV3box+0x10>
    1288:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    128c:	48 89 10             	mov    %rdx,(%rax)
    128f:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1293:	8b 55 f4             	mov    -0xc(%rbp),%edx
    1296:	89 50 08             	mov    %edx,0x8(%rax)
    1299:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    129d:	8b 55 f0             	mov    -0x10(%rbp),%edx
    12a0:	89 50 0c             	mov    %edx,0xc(%rax)
    12a3:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    12a7:	8b 55 ec             	mov    -0x14(%rbp),%edx
    12aa:	89 50 10             	mov    %edx,0x10(%rax)
    12ad:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    12b1:	8b 55 e8             	mov    -0x18(%rbp),%edx
    12b4:	89 50 14             	mov    %edx,0x14(%rax)
    12b7:	90                   	nop
    12b8:	c9                   	leaveq 
    12b9:	c3                   	retq   

00000000000012ba <_ZN3box4dumpEv>:
    12ba:	f3 0f 1e fa          	endbr64 
    12be:	55                   	push   %rbp
    12bf:	48 89 e5             	mov    %rsp,%rbp
    12c2:	48 83 ec 10          	sub    $0x10,%rsp
    12c6:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)
    12ca:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    12ce:	8b 70 14             	mov    0x14(%rax),%esi
    12d1:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    12d5:	8b 48 10             	mov    0x10(%rax),%ecx
    12d8:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    12dc:	8b 50 0c             	mov    0xc(%rax),%edx
    12df:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    12e3:	8b 40 08             	mov    0x8(%rax),%eax
    12e6:	41 89 f0             	mov    %esi,%r8d
    12e9:	89 c6                	mov    %eax,%esi
    12eb:	48 8d 3d 26 0d 00 00 	lea    0xd26(%rip),%rdi        # 2018 <_IO_stdin_used+0x18>
    12f2:	b8 00 00 00 00       	mov    $0x0,%eax
    12f7:	e8 64 fd ff ff       	callq  1060 <printf@plt>
    12fc:	90                   	nop
    12fd:	c9                   	leaveq 
    12fe:	c3                   	retq   
    12ff:	90                   	nop

0000000000001300 <_ZN6sphereC1Eii>:
    1300:	f3 0f 1e fa          	endbr64 
    1304:	55                   	push   %rbp
    1305:	48 89 e5             	mov    %rsp,%rbp
    1308:	48 83 ec 10          	sub    $0x10,%rsp
    130c:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)
    1310:	89 75 f4             	mov    %esi,-0xc(%rbp)
    1313:	89 55 f0             	mov    %edx,-0x10(%rbp)
    1316:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    131a:	48 89 c7             	mov    %rax,%rdi
    131d:	e8 ea fe ff ff       	callq  120c <_ZN6objectC1Ev>
    1322:	48 8d 15 0f 2a 00 00 	lea    0x2a0f(%rip),%rdx        # 3d38 <_ZTV6sphere+0x10>
    1329:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    132d:	48 89 10             	mov    %rdx,(%rax)
    1330:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1334:	8b 55 f4             	mov    -0xc(%rbp),%edx
    1337:	89 50 08             	mov    %edx,0x8(%rax)
    133a:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    133e:	8b 55 f0             	mov    -0x10(%rbp),%edx
    1341:	89 50 0c             	mov    %edx,0xc(%rax)
    1344:	90                   	nop
    1345:	c9                   	leaveq 
    1346:	c3                   	retq   
    1347:	90                   	nop

0000000000001348 <_ZN6sphere4dumpEv>:
    1348:	f3 0f 1e fa          	endbr64 
    134c:	55                   	push   %rbp
    134d:	48 89 e5             	mov    %rsp,%rbp
    1350:	48 83 ec 10          	sub    $0x10,%rsp
    1354:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)
    1358:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    135c:	8b 50 0c             	mov    0xc(%rax),%edx
    135f:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1363:	8b 40 08             	mov    0x8(%rax),%eax
    1366:	89 c6                	mov    %eax,%esi
    1368:	48 8d 3d e1 0c 00 00 	lea    0xce1(%rip),%rdi        # 2050 <_IO_stdin_used+0x50>
    136f:	b8 00 00 00 00       	mov    $0x0,%eax
    1374:	e8 e7 fc ff ff       	callq  1060 <printf@plt>
    1379:	90                   	nop
    137a:	c9                   	leaveq 
    137b:	c3                   	retq   
    137c:	0f 1f 40 00          	nopl   0x0(%rax)

/*
 * arm
 */
000000000040071c <main>:
  40071c:	a9bb7bfd 	stp	x29, x30, [sp, #-80]!
  400720:	910003fd 	mov	x29, sp
  400724:	9100a3a0 	add	x0, x29, #0x28
  400728:	528003c4 	mov	w4, #0x1e                  	// #30
  40072c:	52800283 	mov	w3, #0x14                  	// #20
  400730:	52800142 	mov	w2, #0xa                   	// #10
  400734:	52800021 	mov	w1, #0x1                   	// #1
  400738:	9400002a 	bl	4007e0 <_ZN3boxC1Eiiii>
  40073c:	910063a0 	add	x0, x29, #0x18
  400740:	52800502 	mov	w2, #0x28                  	// #40
  400744:	52800041 	mov	w1, #0x2                   	// #2
  400748:	94000053 	bl	400894 <_ZN6sphereC1Eii>
  40074c:	9100a3a0 	add	x0, x29, #0x28
  400750:	f90027a0 	str	x0, [x29, #72]
  400754:	910063a0 	add	x0, x29, #0x18
  400758:	f90023a0 	str	x0, [x29, #64]
  40075c:	f94027a0 	ldr	x0, [x29, #72]
  400760:	f9400000 	ldr	x0, [x0]
  400764:	f9400001 	ldr	x1, [x0]
  400768:	f94027a0 	ldr	x0, [x29, #72]
  40076c:	d63f0020 	blr	x1
  400770:	f94023a0 	ldr	x0, [x29, #64]
  400774:	f9400000 	ldr	x0, [x0]
  400778:	f9400001 	ldr	x1, [x0]
  40077c:	f94023a0 	ldr	x0, [x29, #64]
  400780:	d63f0020 	blr	x1
  400784:	52800000 	mov	w0, #0x0                   	// #0
  400788:	a8c57bfd 	ldp	x29, x30, [sp], #80
  40078c:	d65f03c0 	ret

0000000000400790 <_ZN6objectC1Ev>:
  400790:	d10043ff 	sub	sp, sp, #0x10
  400794:	f90007e0 	str	x0, [sp, #8]
  400798:	90000000 	adrp	x0, 400000 <_init-0x5a0>
  40079c:	9129e001 	add	x1, x0, #0xa78
  4007a0:	f94007e0 	ldr	x0, [sp, #8]
  4007a4:	f9000001 	str	x1, [x0]
  4007a8:	d503201f 	nop
  4007ac:	910043ff 	add	sp, sp, #0x10
  4007b0:	d65f03c0 	ret

00000000004007b4 <_ZN6object4dumpEv>:
  4007b4:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  4007b8:	910003fd 	mov	x29, sp
  4007bc:	f9000fa0 	str	x0, [x29, #24]
  4007c0:	f9400fa0 	ldr	x0, [x29, #24]
  4007c4:	b9400801 	ldr	w1, [x0, #8]
  4007c8:	90000000 	adrp	x0, 400000 <_init-0x5a0>
  4007cc:	91272000 	add	x0, x0, #0x9c8
  4007d0:	97ffff90 	bl	400610 <printf@plt>
  4007d4:	d503201f 	nop
  4007d8:	a8c27bfd 	ldp	x29, x30, [sp], #32
  4007dc:	d65f03c0 	ret

00000000004007e0 <_ZN3boxC1Eiiii>:
  4007e0:	a9bd7bfd 	stp	x29, x30, [sp, #-48]!
  4007e4:	910003fd 	mov	x29, sp
  4007e8:	f90017a0 	str	x0, [x29, #40]
  4007ec:	b90027a1 	str	w1, [x29, #36]
  4007f0:	b90023a2 	str	w2, [x29, #32]
  4007f4:	b9001fa3 	str	w3, [x29, #28]
  4007f8:	b9001ba4 	str	w4, [x29, #24]
  4007fc:	f94017a0 	ldr	x0, [x29, #40]
  400800:	97ffffe4 	bl	400790 <_ZN6objectC1Ev>
  400804:	90000000 	adrp	x0, 400000 <_init-0x5a0>
  400808:	91298001 	add	x1, x0, #0xa60
  40080c:	f94017a0 	ldr	x0, [x29, #40]
  400810:	f9000001 	str	x1, [x0]
  400814:	f94017a0 	ldr	x0, [x29, #40]
  400818:	b94027a1 	ldr	w1, [x29, #36]
  40081c:	b9000801 	str	w1, [x0, #8]
  400820:	f94017a0 	ldr	x0, [x29, #40]
  400824:	b94023a1 	ldr	w1, [x29, #32]
  400828:	b9000c01 	str	w1, [x0, #12]
  40082c:	f94017a0 	ldr	x0, [x29, #40]
  400830:	b9401fa1 	ldr	w1, [x29, #28]
  400834:	b9001001 	str	w1, [x0, #16]
  400838:	f94017a0 	ldr	x0, [x29, #40]
  40083c:	b9401ba1 	ldr	w1, [x29, #24]
  400840:	b9001401 	str	w1, [x0, #20]
  400844:	d503201f 	nop
  400848:	a8c37bfd 	ldp	x29, x30, [sp], #48
  40084c:	d65f03c0 	ret

0000000000400850 <_ZN3box4dumpEv>:
  400850:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  400854:	910003fd 	mov	x29, sp
  400858:	f9000fa0 	str	x0, [x29, #24]
  40085c:	f9400fa0 	ldr	x0, [x29, #24]
  400860:	b9400801 	ldr	w1, [x0, #8]
  400864:	f9400fa0 	ldr	x0, [x29, #24]
  400868:	b9400c02 	ldr	w2, [x0, #12]
  40086c:	f9400fa0 	ldr	x0, [x29, #24]
  400870:	b9401003 	ldr	w3, [x0, #16]
  400874:	f9400fa0 	ldr	x0, [x29, #24]
  400878:	b9401404 	ldr	w4, [x0, #20]
  40087c:	90000000 	adrp	x0, 400000 <_init-0x5a0>
  400880:	91276000 	add	x0, x0, #0x9d8
  400884:	97ffff63 	bl	400610 <printf@plt>
  400888:	d503201f 	nop
  40088c:	a8c27bfd 	ldp	x29, x30, [sp], #32
  400890:	d65f03c0 	ret

0000000000400894 <_ZN6sphereC1Eii>:
  400894:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  400898:	910003fd 	mov	x29, sp
  40089c:	f9000fa0 	str	x0, [x29, #24]
  4008a0:	b90017a1 	str	w1, [x29, #20]
  4008a4:	b90013a2 	str	w2, [x29, #16]
  4008a8:	f9400fa0 	ldr	x0, [x29, #24]
  4008ac:	97ffffb9 	bl	400790 <_ZN6objectC1Ev>
  4008b0:	90000000 	adrp	x0, 400000 <_init-0x5a0>
  4008b4:	91292001 	add	x1, x0, #0xa48
  4008b8:	f9400fa0 	ldr	x0, [x29, #24]
  4008bc:	f9000001 	str	x1, [x0]
  4008c0:	f9400fa0 	ldr	x0, [x29, #24]
  4008c4:	b94017a1 	ldr	w1, [x29, #20]
  4008c8:	b9000801 	str	w1, [x0, #8]
  4008cc:	f9400fa0 	ldr	x0, [x29, #24]
  4008d0:	b94013a1 	ldr	w1, [x29, #16]
  4008d4:	b9000c01 	str	w1, [x0, #12]
  4008d8:	d503201f 	nop
  4008dc:	a8c27bfd 	ldp	x29, x30, [sp], #32
  4008e0:	d65f03c0 	ret

00000000004008e4 <_ZN6sphere4dumpEv>:
  4008e4:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  4008e8:	910003fd 	mov	x29, sp
  4008ec:	f9000fa0 	str	x0, [x29, #24]
  4008f0:	f9400fa0 	ldr	x0, [x29, #24]
  4008f4:	b9400801 	ldr	w1, [x0, #8]
  4008f8:	f9400fa0 	ldr	x0, [x29, #24]
  4008fc:	b9400c02 	ldr	w2, [x0, #12]
  400900:	90000000 	adrp	x0, 400000 <_init-0x5a0>
  400904:	91284000 	add	x0, x0, #0xa10
  400908:	97ffff42 	bl	400610 <printf@plt>
  40090c:	d503201f 	nop
  400910:	a8c27bfd 	ldp	x29, x30, [sp], #32
  400914:	d65f03c0 	ret


#endif

